#include "WOH_Vertex.h"

WOH_Vertex::WOH_Vertex(LPDIRECT3DDEVICE9	pDevice)
{
	m_pDevice=pDevice;

}

WOH_Vertex::~WOH_Vertex(void)
{
	CloseDown();
}
BOOL WOH_Vertex::CreateVertexBuffer(DWORD dwNumVertices, DWORD dwFVF)
{
    m_FVF=dwFVF;
    m_NumVertices=dwNumVertices;
	m_VertexSize=::D3DXGetFVFVertexSize(m_FVF);
	if(m_NumVertices==0||m_VertexSize==0)
		return FALSE;
	if(FAILED(m_pDevice->CreateVertexBuffer(m_NumVertices*m_VertexSize,0,m_FVF,D3DPOOL_DEFAULT,&m_pVBuffer,NULL)))
		return FALSE;
	else
		return TRUE;
}
BOOL WOH_Vertex::Lock(unsigned long startVertex, unsigned int numVertices)
{
    if(m_pVBuffer==NULL)
		return FALSE;
	if(FAILED(m_pVBuffer->Lock(startVertex*m_VertexSize,numVertices*m_VertexSize,(void**)&m_pData,0)))
		return FALSE;
	else
		return TRUE;
}
BOOL WOH_Vertex::UnLock()
{
    if(m_pVBuffer==NULL)
		return FALSE;
	if(FAILED(m_pVBuffer->Unlock()))
		return FALSE;
	else
		return TRUE;
}
void WOH_Vertex::SendData()
{
   m_pDevice->SetStreamSource(0,m_pVBuffer,0,m_VertexSize);
	m_pDevice->SetFVF(m_FVF);
}
BOOL WOH_Vertex::Render(unsigned long startVertex, unsigned long triCount, DWORD type)
{
    if(m_pVBuffer==NULL)
		return FALSE;
	m_pDevice->DrawPrimitive((D3DPRIMITIVETYPE) type,startVertex,triCount);
	return TRUE;
}
void WOH_Vertex::CloseDown()
{
	m_pData=NULL;
	if(m_pVBuffer)
		m_pVBuffer->Release();
}
